package com.jboa.web.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.jboa.service.IBizClaimVoucherService;
import com.jboa.service.IEmployeeService;
import com.jboa.service.ILeaveService;
import com.jboa.vo.PageHelper;
import com.jboa.vo.SysEmployee;
import com.opensymphony.xwork2.ActionContext;

public class ListAction {
	@Autowired
	@Qualifier("voucherService")
	private IBizClaimVoucherService voucherService;
	@Autowired
	@Qualifier("employeeService")
	private IEmployeeService empService;
	@Autowired
	@Qualifier("LeaveService")
	private ILeaveService leaveService;
	private String statu;
	private Date starttime;
	private Date endtime;
	private PageHelper pageH = new PageHelper();
	private Integer pageNo;
	private Map<String, String> statusmap;
	private Map<String,String> typemap;
	private SysEmployee emp;

	/**
	 * 获取报销单集合
	 * 
	 * @return
	 * @throws ParseException
	 */
	public String getList() throws ParseException {
		Map<String, Object> session = ActionContext.getContext().getSession();
		SysEmployee emp = (SysEmployee) session.get("employee");
		emp = empService.getBysn(emp);
		// 普通员工查看
		if (emp.getSysPosition().getNameCn().equals("员工")) {
			pageH.getParams().put("sn", emp.getSn());
		}
		// 部门经理查看
		if (emp.getSysPosition().getNameCn().equals("部门经理")) {
			pageH.getParams().put("dept", emp.getSysDepartment().getName());
			pageH.getParams().put("dealEmployee", emp.getSn());
		}
		// 总经理查看
		if (emp.getSysPosition().getNameCn().equals("总经理")) {
			pageH.getParams().put("dealEmployee", emp.getSn());
		}
		// 状态
		if (statu != null && !statu.equals("")) {
			pageH.getParams().put("statu", statu);
		}
		// 开始时间
		if (starttime != null && !starttime.equals("")) {
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			starttime = f.parse(f.format(starttime));
			pageH.getParams().put("starttime", starttime);
		}
		// 结束时间
		if (endtime != null && !endtime.equals("")) {
			pageH.getParams().put("endtime", endtime);
		}
		if(pageNo!=null){
			pageH.setPageno(pageNo);
		}
		voucherService.getBypage(pageH);

		// 初始化状态信息
		this.look();

		return "success";
	}

	// 初始化状态信息
	public void look() {
		statusmap = new HashMap<String, String>();
		statusmap.put("新创建", "新创建");
		statusmap.put("已提交", "已提交");
		statusmap.put("已审批", "已审批");
		statusmap.put("已付款", "已付款");
		statusmap.put("已打回", "已打回");
		statusmap.put("已终止", "已终止");
	}

	// 获取添加请假单页面需要的信息
	public String getLeaveInfo() {
		typemap=new HashMap<String, String>();
		// 休假类型
		typemap.put("年假", "年假");
		typemap.put("病假", "病假");
		typemap.put("婚假", "婚假");
		// 下一处理人
		// 获取该部门经理
		SysEmployee loginemp = (SysEmployee) ActionContext.getContext()
				.getSession().get("employee");
		emp = empService.getNextDealEmployee("部门经理",
				loginemp.getSysDepartment());
		return "success";
	}
	
	/**
	 *获取请假单列表 
	 * @throws ParseException 
	 */
	public String getLeaveList() throws ParseException{
		Map<String, Object> session = ActionContext.getContext().getSession();
		SysEmployee emp = (SysEmployee) session.get("employee");
		emp = empService.getBysn(emp);
		// 普通员工查看
		if (emp.getSysPosition().getNameCn().equals("员工")) {
			pageH.getParams().put("sn", emp.getSn());
		}
		// 部门经理查看
		if (emp.getSysPosition().getNameCn().equals("部门经理")) {
			pageH.getParams().put("dept", emp.getSysDepartment().getName());
			pageH.getParams().put("dealEmployee", emp.getSn());
			//pageH.getParams().put("statu", "待审批");
		}
		// 开始时间
		if (starttime != null && !starttime.equals("")) {
			SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
			starttime = f.parse(f.format(starttime));
			pageH.getParams().put("starttime", starttime);
		}
		// 结束时间
		if (endtime != null && !endtime.equals("")) {
			pageH.getParams().put("endtime", endtime);
		}
		if(pageNo!=null){
			pageH.setPageno(pageNo);
		}
		leaveService.getByPage(pageH);
		return "success";
		
	}

	public String getStatu() {
		return statu;
	}

	public void setStatu(String statu) {
		this.statu = statu;
	}

	public Date getStarttime() {
		return starttime;
	}

	public void setStarttime(Date starttime) {
		this.starttime = starttime;
	}

	public Date getEndtime() {
		return endtime;
	}

	public void setEndtime(Date endtime) {
		this.endtime = endtime;
	}

	public PageHelper getPageH() {
		return pageH;
	}

	public void setPageH(PageHelper pageH) {
		this.pageH = pageH;
	}

	public Map<String, String> getStatusmap() {
		return statusmap;
	}

	public void setStatusmap(Map<String, String> statusmap) {
		this.statusmap = statusmap;
	}

	public Map<String, String> getTypemap() {
		return typemap;
	}

	public void setTypemap(Map<String, String> typemap) {
		this.typemap = typemap;
	}

	public SysEmployee getEmp() {
		return emp;
	}

	public void setEmp(SysEmployee emp) {
		this.emp = emp;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

}
